package com.erp.servlet;

import com.erp.bean.Emp;
import com.erp.bean.Role;
import com.erp.service.EmpService;
import com.erp.service.RoleService;
import com.erp.service.impl.EmpServiceImpl;
import com.erp.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zch
 * 人员角色分配控制器
 */
@WebServlet("/emprole.action")
public class EmpRoleTodeptServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建员工&角色业务逻辑对象
        EmpService empService = new EmpServiceImpl();
        RoleService roleService = new RoleServiceImpl();
        //接收type
        String type = req.getParameter("type");
        //接收用户传入的员工编号
        String eidstr = req.getParameter("eid");
        //强制类型转换
        int eid = Integer.valueOf(eidstr);
        System.out.println(eid);
        Emp emp = empService.findById(eid);
        if(type.equals("1")){
            List<Role> list = roleService.findAll();
            req.setAttribute("rolelist",list);
            req.setAttribute("emp",emp);
            req.getRequestDispatcher("emproletodept.jsp").forward(req,resp);
        }else if(type.equals("2")){
            String ridstr = req.getParameter("rid");
            int rid = Integer.valueOf(ridstr);
            Emp emp1 = new Emp();
            emp1.setEid(eid);
            emp1.setDid(emp.getDid());
            emp1.setRid(rid);
            emp1.setEname(emp.getEname());
            emp1.setAge(emp.getAge());
            emp1.setSex(emp.getSex());
            emp1.setPass(emp.getPass());
            emp1.setEmail(emp.getEmail());
            emp1.setTel(emp.getTel());
            System.out.println(emp1);
            EmpService empService1 = new EmpServiceImpl();
            boolean flag = empService1.update(emp1);
            System.out.println(flag);
            resp.sendRedirect("emplist.action");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
